/**
 *
 */
package com.landmark;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * @author Kiran Kumar Motapothula
 *
 */
public class RemoveDuplicatesSKURows extends JPanel {

    /**
     *
     */
    public void start() throws Exception {
        ReadInputFile();
    }

    private void ReadInputFile() throws Exception {
        // User file path inputs
        String inputFilePath = Util.getHelperBean().getInputFile(); // read input file path
        String outputFilePath = Util.getHelperBean().getOutputFilePath();// read output file path

        File inputFile = new File(inputFilePath);
        BufferedReader inputScan = null; // Scanner is used to read a whole line or character from the input file 
        java.util.Hashtable<String, String> skuIds = new java.util.Hashtable<String, String>();

        if (inputFile.exists()) {
            inputScan = new BufferedReader(new FileReader(inputFile));
            FileWriter fw = new FileWriter(outputFilePath);// file path from user
            String line;
            while ((line = inputScan.readLine()) != null) {

                if (inLineCount > 0) {
                    String[] splitLine = line.split(",");
                    String skuId = splitLine[4];
                    if (skuIds.get(skuId) == null) {
                        outLineCount++;
                        fw.write(line);
                        fw.write("\n");
                        fw.flush();
                    }
                    skuIds.put(skuId, skuId.toString());

                } else {
                    fw.write(line);
                    fw.write("\n");
                    fw.flush();
                }
                inLineCount++;
            }//close while( inputScan.hasNext())		
            inputScan.close();
            fw.close();
            System.out.println(skuIds.size());
        }// close if( inputFile.exists())

        JOptionPane.showMessageDialog(this, "File genareted with total lines " + outLineCount + " / " + inLineCount + " lines at " + outputFilePath, "Done", JOptionPane.INFORMATION_MESSAGE);
    }
    int outLineCount = 0;
    int inLineCount = 0;
}
